import ResList from "../ResList";

/**
 * 资源缓存池.
 * @deprecated 废弃.cocos有自带的节点池.cc.NodePool
 */

export class ResPool{
    //对象池.
    private enemy1Pool = new cc.NodePool();
    private playerBulletPool = new cc.NodePool();
    private playerPool = new cc.NodePool();

    //预制件
    playerPrefab:cc.Prefab;
    enemy1Prefab:cc.Prefab;
    playerBulletPrefab:cc.Prefab;

    /**
     * 使用预制件资源,初始化资源缓存池.
     * @param playerPrefab 
     * @param enemy1Prefab 
     * @param playerBulletPrefab 
     */
    constructor(){
        let resList = this.getResList();

        this.playerPrefab = resList.playerPrefab;
        this.enemy1Prefab = resList.enemy1Prefab;
        this.playerBulletPrefab = resList.playerBulletPrefab;
    }
    /**
     * 获取项目中各种资源的引用.
     */
    getResList():ResList{
        let canvasNode = cc.director.getScene().getChildByName('Canvas');
        return canvasNode.getComponent(ResList);
    }
    /**
     * 从对象缓存池中取一个 player 节点对象.
     */
    getPlayer():cc.Node{
        let player = null
        if(this.playerPool.size()>0){
            player = this.playerPool.get()
        }else{
            player = cc.instantiate(this.playerPrefab)
        }

        return player;
    }
    /**
     * 从对象缓存池中取一个 enemy1 节点对象.
     */
    getEnemy1():cc.Node{
        let enemy1 = null
        if(this.enemy1Pool.size()>0){
            enemy1 = this.enemy1Pool.get()
        }else{
            enemy1 = cc.instantiate(this.enemy1Prefab)
        }

        return enemy1;
    }
    /**
     * 从对象缓存池中取一个 bullet 节点对象.
     */
    getBullet():cc.Node{
        let bullet = null
        if(this.playerBulletPool.size()>0){
            bullet = this.playerBulletPool.get()
        }else{
            bullet = cc.instantiate(this.playerBulletPrefab)
        }

        return bullet;
    }
    /**
     * 将资源回到到缓存池中.
     * @param player 
     */
    putPlayer(player:cc.Node){
        this.playerPool.put(player);
    }
    /**
     * 将资源回到到缓存池中.并自动从父节点上移除.
     * 参考:cc.NodePool.put(obj: cc.Node)
     * @param enemy1 
     */
    putEnemy1(enemy1:cc.Node){
        this.enemy1Pool.put(enemy1);
    }
    /**
     * 将资源回到到缓存池中.
     * @param playerBullet 
     */
    putBullet(playerBullet:cc.Node){
        this.playerBulletPool.put(playerBullet);
    }

    clear(){
        this.playerPool.clear();
        this.playerBulletPool.clear();
        this.enemy1Pool.clear();
    }
}